package com.wthink.newMain.newBusiness

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.FragmentNewBusinessBinding
import com.wthink.newMain.main.MainNewActivity
import com.wthink.newMain.newBusiness.inner.BusinessHistoryFragment
import com.wthink.newMain.newBusiness.inner.BusinessUpFragment

class NewBusinessFragment : Fragment(){
    private val viewModel by viewModels<NewBusinessViewmodel>()

    private lateinit var binding: FragmentNewBusinessBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_new_business,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity!!.supportFragmentManager
            .beginTransaction()
            .add(R.id.content, BusinessUpFragment())
            .commit()


        binding.businessButton.isSelected = true

        binding.businessButton.setOnClickListener {
            addFragment(BusinessUpFragment())
            binding.businessButton.isSelected = true
            binding.upHistory.isSelected = false
        }

        binding.upHistory.setOnClickListener {
            addFragment(BusinessHistoryFragment())
            binding.businessButton.isSelected = false
            binding.upHistory.isSelected = true
        }

        viewModel.start()
    }

    private fun addFragment(fragment: Fragment) {
        activity!!.supportFragmentManager
            .beginTransaction()
            //.setCustomAnimations(R.anim.design_bottom_sheet_slide_in, R.anim.design_bottom_sheet_slide_out)
            .replace(R.id.content, fragment, fragment.javaClass.simpleName)
            .addToBackStack(fragment.javaClass.simpleName)
            .commit()
    }

    override fun onResume() {
        super.onResume()
        Log.d("resume", "NewBusinessFragmentresume")
        val mainNewActivity = activity as MainNewActivity
        mainNewActivity.binding.ivSchedle.isSelected = false
        mainNewActivity.binding.ivCamera.isSelected = false
        mainNewActivity.binding.ivServer.isSelected = false
        mainNewActivity.binding.ivBusiness.isSelected = true
        mainNewActivity.binding.ivMsg.isSelected = false
    }
}